<template>
  <div>
    <el-form :inline="true" class="demo-form-inline" >
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="cForm.category1Id" @change="getCategory2List" :disabled='can'>
          <el-option :label="c1.name" :value="c1.id" v-for="c1 in List1" :key="c1.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="cForm.category2Id" @change="getCategory3List" :disabled='can'>
          <el-option :label="c2.name" :value="c2.id" v-for="c2 in List2" :key="c2.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="cForm.category3Id" @change="getAttr" :disabled='can'>
          <el-option :label="c3.name" :value="c3.id" v-for="c3 in List3" :key="c3.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'CategorySelect',
  props:['can'],
  data() {
    return {
      //三级分类的列表数据
      List1:[],
      List2:[],
      List3:[],
      //收集三级分类的Id
      cForm:{
        category1Id:'',
        category2Id:'',
        category3Id:'',
      }
    }
  },
  mounted(){
    this.getCategory1List()
  },
  methods: {
    //获取一级分类数据
    async getCategory1List(){
      const result =await this.$API.attr.reqCategory1List()
      if(result.code==200){
        this.List1=result.data
      }else{
        this.$message.error('获取数据失败')
      }
    },
    //获取二级分类数据
    async getCategory2List(){
      //清除之前的数据
      this.List2=[]
      this.List3=[]
      this.cForm.category2Id=''
      this.cForm.category3Id=''
      //将一级分类Id传给父组件
      this.$emit('getCategoryId',{getCategoryId:this.cForm.category1Id,level:1})
      //获取二级分类数据
      const result = await this.$API.attr.reqCategory2List(this.cForm.category1Id)
      if(result.code==200){
        this.List2=result.data
      }else{
        this.$message.error('获取数据失败')
      }
    },
    //获取三级分类数据
    async getCategory3List(){
      //清除之前的数据
      this.List3=[]
      this.cForm.category3Id=''
      //将二级分类Id传给父组件
      this.$emit('getCategoryId',{getCategoryId:this.cForm.category2Id,level:2})
      //获取三级分类数据
      const result =await this.$API.attr.reqCategory3List(this.cForm.category2Id)
      if(result.code==200){
        this.List3=result.data
      }else{
        this.$message.error('获取数据失败')
      }
    },
    //根据id获取属性
    async getAttr(){
      //将三级分类Id传给父组件
      this.$emit('getCategoryId',{getCategoryId:this.cForm.category3Id,level:3})
    }
  },
}
</script>

<style></style>
